package com.netdisk.demo.controller;

import com.netdisk.demo.entity.Authorities;
import com.netdisk.demo.entity.File;
import com.netdisk.demo.entity.Folder;
import com.netdisk.demo.entity.User;
import com.netdisk.demo.security.MyUserDetails;
import com.netdisk.demo.service.AuthoritiesService;
import com.netdisk.demo.service.FileService;
import com.netdisk.demo.service.FolderService;
import com.netdisk.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.Collection;
import java.util.List;

/**
 * @Author Wang
 * @Description 网页跳转
 * @Date 22:25 2020/5/10
 **/
@Controller
public class WebController {
    @RequestMapping({"/login"})
    public String login() throws Exception{
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();

        if (!(auth instanceof AnonymousAuthenticationToken)) {
            //用户已登录
            return "redirect:/index";
        }
        return "login";
    }
    //跳转主页
    @Autowired
    private UserService userService;
    @Autowired
    private AuthoritiesService authoritiesService;
    @RequestMapping({"/","/index"})
    public String index(){
        return "index";
    }
    @RequestMapping("/emps")
    public String emps(Model model){
        List<User> emp = userService.queryAllByLimit(0,50);
        List<Authorities> group = authoritiesService.queryAllAuthority();
        System.out.print(group.get(1));
        model.addAttribute("emps",emp);
        model.addAttribute("group",group);
        return "emp/list";
    }

    @Autowired
    private FileService fileService;
    @Autowired
    private FolderService folderService;
    /**
     * @Author Wang
     * @Description 文件列表实现
     * @Date 0:08 2020/5/11
     * @Param [model]
     * @return java.lang.String
     **/
    @RequestMapping("/file")
    public String file(Model model, @RequestParam(value = "folder",defaultValue = "0")int i){
        File file = new File();
        Folder folder=new Folder();         //新建文件和文件夹实体
        file.setFolderId(i);
        folder.setParentId(i);              //查询当前文件夹下的文件
        Collection<File> files=fileService.queryAll(file);
        Collection<Folder> folders=folderService.queryAll(folder);  //生成文件、文件夹列表
        System.out.println("正在查询文件列表");
        model.addAttribute("folders",folders);
        model.addAttribute("files",files);              //通过model传到前端
        if(!files.isEmpty()){
            System.out.println("查询成功");
        }
        return "FileExplorer/FileList";
    }

    @RequestMapping("/myfile")
    public String myfile(HttpServletRequest request, Model model, @RequestParam(value = "folder",defaultValue = "0")int i){
        File file = new File();
        Folder folder = new Folder();
        file.setFolderId(i);
        file.setUserId(request.getRemoteUser());
        Collection<File> files=fileService.queryAll(file);
        Collection<Folder> folders=folderService.queryAll(folder);
        System.out.println("正在查询用户文件列表");
        model.addAttribute("folders",folders);
        model.addAttribute("myfiles",files);
        if(!files.isEmpty()){
            System.out.println("查询成功");
        }
        return "FileExplorer/MyFileList";
    }

}
